﻿namespace WMS6.Web.Outbound
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.OutboundServiceRef;
    using WMS6.Web.UserControls;

    public class AllocationList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected HtmlInputButton btnReport;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F7042;
        protected GridView gvAllocation;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblAllocationList;
        protected Label lblMsg;
        protected LinkButton lnkbtnAllocation;
        protected LinkButton lnkbtnOutboundPack;
        protected LinkButton lnkbtnOutboundReview;
        protected LinkButton lnkbtnPrintExpress;
        protected LinkButton lnkbtnPrintHandOver;
        protected LinkButton lnkbtnPrintOrderInvoice;
        protected LinkButton lnkbtnPrintPickByLack;
        protected LinkButton lnkbtnPrintShippingOrder;
        protected LinkButton lnkbtnWeightReview;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnAllocation;
        protected HtmlTableCell tdlnkbtnOutboundPack;
        protected HtmlTableCell tdlnkbtnOutboundReview;
        protected HtmlTableCell tdlnkbtnPrintExpress;
        protected HtmlTableCell tdlnkbtnPrintHandOver;
        protected HtmlTableCell tdlnkbtnPrintOrderInvoice;
        protected HtmlTableCell tdlnkbtnPrintPickByLack;
        protected HtmlTableCell tdlnkbtnPrintShippingOrder;
        protected HtmlTableCell tdlnkbtnWeightReview;
        protected UpdatePanel up1;

        private void BindDataOrderList(OrdersQueryEntity orderQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(orderQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OutboundServiceClientProxy.GetInstance().GetOrdersInfoByQueryEntity(orderQuery);
            base.BindData2GridView(this.gvAllocation, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvAllocation, "ORDER_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7400_020");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F7400_021");
            }
            else
            {
                base.Response.Redirect(string.Format("Allocation.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
            orderQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = orderQuery.CurrentPage;
            orderQuery.CurrentPage = "0";
            this.gvAllocation.Columns[1].Visible = false;
            this.gvAllocation.AllowSorting = false;
            this.BindDataOrderList(orderQuery);
            this.hdnActionID.Value = "";
            this.gvAllocation.ShowFooter = false;
            orderQuery.IsGetAll = false;
            orderQuery.CurrentPage = currentPageIndex;
            this.SetOrderQueryEntity(orderQuery);
            base.Export2Excel("Orders.xls", this.gvAllocation);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
            orderQuery.CurrentPage = "0";
            this.BindDataOrderList(orderQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
            orderQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataOrderList(orderQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity ordersQuery = this.GetOrderQueryEntity();
            ordersQuery.OrderID = "";
            ordersQuery.OwnerID = "";
            ordersQuery.ExternOrderID = "";
            ordersQuery.OrderDate = "";
            ordersQuery.RequestedShipDate = "";
            ordersQuery.ActualShipDate = "";
            ordersQuery.DeliveryDate = "";
            ordersQuery.Status = "";
            ordersQuery.Type = "";
            ordersQuery.ConsigneeID = "";
            ordersQuery.CCompany = "";
            ordersQuery.ReferenceNum = "";
            this.BindDataOrderList(ordersQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
            if (int.Parse(orderQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                orderQuery.CurrentPage = (int.Parse(orderQuery.CurrentPage) + 1).ToString();
                this.BindDataOrderList(orderQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
            if (int.Parse(orderQuery.CurrentPage) > 0)
            {
                orderQuery.CurrentPage = (int.Parse(orderQuery.CurrentPage) - 1).ToString();
                this.BindDataOrderList(orderQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
            this.BindDataOrderList(orderQuery);
        }

        private OrdersQueryEntity GetOrderQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as OrdersQueryEntity);
            }
            return (base.PageQueryEntitySession as OrdersQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
                orderQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataOrderList(orderQuery);
            }
        }

        protected void gvAllocation_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F7042", this.gvAllocation);
        }

        protected void gvAllocation_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblREMAEK"));
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblCCompany"));
                base.BindingOrdersPriority((Image) e.Row.FindControl("imgPriority"), (Label) e.Row.FindControl("lblPriority"), this.gvAllocation.DataKeys[e.Row.RowIndex]["PRIORITY"].ToString(), (string) this.ViewState["PriorityFlag"]);
            }
        }

        protected void gvAllocation_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                OrdersQueryEntity orderEntity = this.GetOrderQueryEntity();
                orderEntity.SortField = e.SortExpression;
                if (orderEntity.SortDirection == "ASC")
                {
                    orderEntity.SortDirection = "DESC";
                }
                else if (orderEntity.SortDirection == "DESC")
                {
                    orderEntity.SortDirection = "ASC";
                }
                this.BindDataOrderList(orderEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnAllocation_Click(object sernder, EventArgs e)
        {
            this.lblMsg.Text = string.Empty;
            try
            {
                List<string> orderIDS = new List<string>();
                for (int i = 0; i < this.gvAllocation.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvAllocation.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        orderIDS.Add(this.gvAllocation.DataKeys[i]["ORDER_ID"].ToString());
                    }
                }
                if ((orderIDS == null) || (orderIDS.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F7200_009");
                }
                else
                {
                    string result = OutboundServiceClientProxy.GetInstance().AutoAllocate(orderIDS, base.UserInfo.UserID);
                    if (result.EndsWith(","))
                    {
                        result = result.Substring(0, result.Length - 1);
                    }
                    this.lblMsg.Text = result;
                    OrdersQueryEntity orderQuery = this.GetOrderQueryEntity();
                    this.BindDataOrderList(orderQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnOutboundPack_Click(object sender, EventArgs e)
        {
        }

        protected void lnkbtnOutboundReview_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("OutboundReview.aspx", false);
        }

        protected void lnkbtnWeightReview_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("OutboundWeightReview.aspx", false);
        }

        private void LoadData()
        {
            this.ViewState["PriorityFlag"] = base.GetValueFromAppSettings("PriorityFlag");
            OrdersQueryEntity orderQuery = new OrdersQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    orderQuery.OrderID = this.PreviousPage.OrderID;
                    orderQuery.OwnerID = this.PreviousPage.OwnerID;
                    orderQuery.ExternOrderID = this.PreviousPage.ExternOrderID;
                    orderQuery.OrderDate = this.PreviousPage.OrderDate;
                    orderQuery.RequestedShipDate = this.PreviousPage.RequestedShipDate;
                    orderQuery.ActualShipDate = this.PreviousPage.ActualShipDate;
                    orderQuery.DeliveryDate = this.PreviousPage.DeliveryDate;
                    orderQuery.Status = this.PreviousPage.Status;
                    orderQuery.Type = this.PreviousPage.Type;
                    orderQuery.ConsigneeID = this.PreviousPage.CustomerID;
                    orderQuery.CCompany = this.PreviousPage.CustomerName;
                    orderQuery.ReferenceNum = this.PreviousPage.ReferenceNum;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            orderQuery.IsGetAll = false;
            orderQuery.CurrentPage = "0";
            orderQuery.PageSize = this.gvAllocation.PageSize.ToString();
            orderQuery.SortField = "O.ORDER_ID";
            orderQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvAllocation.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetOrderQueryEntity(orderQuery);
            this.BindDataOrderList(orderQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetOrderQueryEntity(OrdersQueryEntity orderQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = orderQuery;
            }
            else
            {
                base.PageQueryEntitySession = orderQuery;
            }
        }

        public AllocationQuery PreviousPage
        {
            get
            {
                return (AllocationQuery) base.PreviousPage;
            }
        }
    }
}

